<?php
class ApplyController extends EController
{
    public $layout = 'main';
    //过滤器
    public function filters()
    {
        return array(
            array(
                'application.filters.BackEndAuthRequestFilter',
            )
        );
    }

    public function actionlist()
    {

        $cookie = new CHttpCookie('ApplyController', str_replace(Yii::app()->request->baseUrl."/","",$_SERVER["REQUEST_URI"]));
        $cookie->expire = time()+60*60*24*30;
        Yii::app()->request->cookies['aiuserhttpbor']=$cookie;
        $this->layout='list';
        $this->son_menu=0;
        $act_list=ActionMenuHelper::getHiddenMenu();
        $model=new ActApply('search');
        if(isset($_GET['ActApply'])){
            $model->attributes=$_GET['ActApply'];
        }
        $model->sh_status=1;

        $this->render('list',array(
                'dataProvider'=>$model->search(),
                'act_list'=>$act_list,
                'model'=>$model)
        );
    }


    public function actionPass(){

        $cookie = Yii::app()->request->getCookies();

        $admin_id = $this->_getId();
        $obj = ActApply::model()->findByPk($admin_id);
        $obj->status=1;
        $obj->sh_status=1;
        $obj->save();

        $obj1 = shopUser::model()->findByPk($obj->user_id);
        $obj2 = ActList::model()->findByPk($obj->act_id);


        ShopMessage::addMessage("applySuc","活动审核成功","您申请的{".$obj2->title."}通过审核，请进入我的活动查看活动准备信息!",$obj->user_id,$obj1->shop_id);

        $this->showMessage(Yii::t('info','operation success'),$cookie['ApplyController']->value);
    }

    public function actionReject(){
        $this->layout='pop';
        $admin_id = $this->_getId();
        $admin = ActApply::model()->findByPk($admin_id);
        $model = new RejectForm();
        $model->attributes = $admin->attributes;
        //ajax 验证
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if(isset($_POST['RejectForm']))
        {
            $model->attributes=$_POST['RejectForm'];
            // 验证用户输入，并在判断输入正确后重定向到

            if($model->validate())
            {
                if($model->edit($admin_id))
                {
                    Yii::app()->user->setFlash('success',Yii::t('info','operation success'));
                }
                else
                {
                    Yii::app()->user->setFlash('failed',Yii::t('info','operation failed'));
                }
                $this->refresh();
            }
        }
        $this->render('reject',array('model'=>$model));
    }

    public function actionSend()
    {
        $id = $this->_getId();
        $order = ActApply::model()->findByPk($id);

        $this->layout='list';
        $this->son_menu=1;
        $model = new OrderSendForm;
        $model->express_company_code = $order->express_company_code;
        $model->express_no = $order->express_no;
        //ajax 验证
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }





        if(isset($_POST['OrderSendForm']))
        {
            $model->attributes=$_POST['OrderSendForm'];
            // 验证用户输入，并在判断输入正确后重定向到
            if($model->validate())
            {
                if($model->send($id))
                {
                    Yii::app()->user->setFlash('success',Yii::t('info','operation success'));
                }
                else
                {
                    Yii::app()->user->setFlash('failed',Yii::t('info','operation failed'));
                }
            }
        }
      //  $this->render('send',array('model'=>$model));



        if ($order->act->real_material) {
            $goodsInfo = json_decode($order->act->real_material, true);
        } else {
            $goodsInfo=[];
        }

        $address =  $order->user->shop->province.$order->user->shop->city.$order->user->shop->district.$order->user->shop->connect_address;

        $this->render('send',array('model'=>$model,"count"=>$order->act_num,'goodsInfo'=>$goodsInfo,'name'=>$order->user->connect_name,"phone"=>$order->user->connect_phone,"addr" =>$address,"order"=>$order));


    }



    //获取id
    private function _getId(){
        if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
            throw new CHttpException(400,'Invalid request. id is required!');
        }
        return $_GET['id'];
    }

    public function actionInfo() {
        $id = $this->_getId();
        $order = ActApply::model()->findByPk($id);
        if ($order->act->real_material) {
            $goodsInfo = json_decode($order->act->real_material, true);
        } else {
            $goodsInfo=[];
        }

        $address =  $order->user->shop->province.$order->user->shop->city.$order->user->shop->district.$order->user->shop->connect_address;

       $this->render('info',array("count"=>$order->act_num,'goodsInfo'=>$goodsInfo,'name'=>$order->user->connect_name,"phone"=>$order->user->connect_phone,"addr" =>$address,"order"=>$order));
    }



    public function actionEnd(){
        $this->layout='pop';
        $admin_id = $this->_getId();
        $admin = ActApply::model()->findByPk($admin_id);
        $model = new EndForm();
        $model->attributes = $admin->attributes;
        $model->status = 3;
        //ajax 验证
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if(isset($_POST['EndForm']))
        {
            $model->attributes=$_POST['EndForm'];
            // 验证用户输入，并在判断输入正确后重定向到

            if($model->validate())
            {
                if($model->edit($admin_id))
                {
                    Yii::app()->user->setFlash('success',Yii::t('info','operation success'));
                }
                else
                {
                    Yii::app()->user->setFlash('failed',Yii::t('info','operation failed'));
                }
                $this->refresh();
            }
        }
        $this->render('end',array('model'=>$model));
    }



    public function actionEndcancel(){
        $this->layout='pop';
        $admin_id = $this->_getId();
        $admin = ActApply::model()->findByPk($admin_id);
        $model = new EndForm();
        $model->attributes = $admin->attributes;
        $model->status = 3;
        //ajax 验证
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if(isset($_POST['EndForm']))
        {
            $model->attributes=$_POST['EndForm'];
            // 验证用户输入，并在判断输入正确后重定向到

            if($model->validate())
            {
                if($model->cancel($admin_id))
                {
                    Yii::app()->user->setFlash('success',Yii::t('info','operation success'));
                }
                else
                {
                    Yii::app()->user->setFlash('failed',Yii::t('info','operation failed'));
                }
                $this->refresh();
            }
        }
        $this->render('endcancel',array('model'=>$model));
    }
}